# -*- coding: utf-8 -*-
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
import time
from pubsub import pub
from zhi_dao.zhidao_const import event_ui_exit
from zhi_dao.img_identify.img_identifier import ZhiDaoImgIdentifier

from zhi_dao import driver
from zhi_dao.ai.zhidao_img_location_reader import ZhiDaoImgLocationReader


class ZhiDaoLogin:
    username: str
    password: str

    def __init__(self, username: str, password: str):
        self.username = username
        self.password = password
        pub.subscribe(self.exit, event_ui_exit)

    def login(self):
        # 打开课程网址
        driver.get("https://passport.zhihuishu.com/login?service="
                   "https://onlineservice-api.zhihuishu.com/gateway/f/v1/login/gologin")
        driver_action = ActionChains(driver)
        wait = WebDriverWait(driver, 100)
        # 登录
        username = wait.until(ec.presence_of_element_located((By.XPATH, "//input[@id='lUsername']")))
        username.send_keys(self.username)
        password = wait.until(ec.presence_of_element_located((By.XPATH, "//input[@id='lPassword']")))
        password.send_keys(self.password)
        commit_btn = driver.find_elements(By.CLASS_NAME, "wall-sub-btn")
        driver_action.click(commit_btn[0]).perform()
        print("登录确定：")
        safe_verify_div = wait.until(ec.presence_of_element_located((By.XPATH, "//div[@class='yidun_slide_indicator']")))
        wait.until(ec.presence_of_element_located((By.XPATH, "//div[@class='yidun_tips']")))
        wait.until(ec.presence_of_element_located((By.XPATH, "//div[@class='yidun_tips__content']")))
        wait.until(ec.presence_of_element_located((By.XPATH, "//span[@class='yidun_tips__point']")))
        wait.until(ec.presence_of_element_located((By.XPATH, "//img[@class='yidun_tips__img']")))
        wait.until(ec.presence_of_element_located((By.XPATH, "//img[@class='yidun_jigsaw']")))
        wait.until(ec.presence_of_element_located((By.XPATH, "//img[@class='yidun_bg-img']")))
        while True:
            # 获取验证码图片
            safe_verify_bg_img = None
            try:
                safe_verify_bg_img = driver.find_element(By.XPATH, "//img[@class='yidun_bg-img']")
            except Exception as e:
                print(e)
            if safe_verify_bg_img is None:
                break
            # else:
            #     # 检查是否不需要验证码
            #     driver.find_element(By.ID, "titleListAll")
            #wait.until(ec.presence_of_element_located((By.XPATH, "//img[@class='yidun_bg-img']")))
            # 获取验证码图片链接
            bg_img_url = safe_verify_bg_img.get_attribute("src")
            if bg_img_url is None:
                continue
            # 获取滑块url
            safe_verify_slide_img = driver.find_element(By.XPATH, "//img[@class='yidun_jigsaw']")
            slide_img_url = safe_verify_slide_img.get_attribute("src")

            # 查看滑块位置
            # x_offset = ZhiDaoImgLocationReader().read(img_url)
            x_offset = ZhiDaoImgIdentifier().read_x_offset(bg_img_url, slide_img_url)
            # x_offset = int(x_offset * 320 / 1000)
            print(x_offset)
            ActionChains(driver).drag_and_drop_by_offset(safe_verify_div, x_offset + 8, 0).perform()
            time.sleep(3)



    @staticmethod
    def exit():
        print("退出chrome driver")
        driver.quit()
